﻿using BZ.Logger;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.Monitor;
using NDK.Motion.Sys;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows;
using System.Windows.Input;

namespace NDK.Module.StationLogic
{
    public class Station_转盘一穴 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_转盘一穴 Instance = new Station_转盘一穴();

        public bool IsGrr反向取料;
        public ConcurrentQueue<MeasureProduct> ValueQueue;
        public Stopwatch SunWay01 = new Stopwatch();
        public string StationName = "Station01转盘1工位";
        //步骤字段、属性
        private StationStep _Step = StationStep.判断是否工作;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if(_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            StationID = 1;
            CurHoleId = 1;
            IsGrr反向取料 = false;
            logBaseInfo = "转盘一穴流程";
            FlowCT = new Stopwatch();
            ValueQueue = new ConcurrentQueue<MeasureProduct> ();
            QueueImage = new ConcurrentQueue<MeasureImage>();
            Step = StationStep.判断是否工作;
            DataProcess.Instance.DatasDic = new ConcurrentDictionary<string, MeasureProduct>();
            DataProcess.Instance.DataResult = new ConcurrentQueue<int>();
        }

        public override void ActionProcess ()
        {
            try
            {
                switch(Step)
                {
                    case StationStep.判断是否工作:
                        if(!Common.AllStationCanWork || Common.DicStationWorkStatus[StationID])
                            break;
                        if(!Common.DicStationWorkStatus[StationID])
                        {
                            if(Common.GRR循环模式 && Common.DicHoleCanWork[CurHoleId] && !Common.DicHoleShield[CurHoleId])
                            {
                                IsGrr反向取料 = true;
                                Common.CurGRR循环次数++;
                            }
                            Step = !Common.DicHoleShield[CurHoleId] ? StationStep.允许机械手放料 : StationStep.工位流程完成;
                        }
                        break;

                    case StationStep.允许机械手放料:
                        if(PLCWriteMethod(logBaseInfo, (Common.PLCAdr.DicOutPutBool[PLCOutPutBool.上料放料允许], true, null)))
                        {
                            Step = StationStep.等待机械手放料;
                        }
                        break;

                    case StationStep.等待机械手放料:
                        if(Common.IsClearModel && !Common.TurnTableNone)
                        {
                            Common.OmronPLC.Write(Common.PLCAdr.DicOutPutBool[PLCOutPutBool.上料放料允许], false);
                            Step = StationStep.工位流程完成;
                            break;
                        }
                        if(Common.PLCAdr.DicInPutValue[PLCInPut.上料到位] || Common.BP_屏蔽PLC || Common.StartOnce)
                        {
                            Step = StationStep.打开真空吸;
                        }
                        break;

                    case StationStep.打开真空吸:
                        if(VacuumMethod(!IsGrr反向取料, (Machine.VacuunArray_转盘真空吸[CurHoleId - 1], Machine.VacuunArray_转盘真空检测[CurHoleId - 1], !Common.IsEmptyModel)))
                        {
                            if (IsGrr反向取料)
                            {
                                Machine.VacuunArray_转盘真空破[CurHoleId - 1].ON();
                                Thread.Sleep(200);
                                Machine.VacuunArray_转盘真空破[CurHoleId - 1].OFF();
                            }
                            if(Common.StartOnce)
                            {
                                Common.StartOnce = false;
                                Station_PPG测量.Instance.IsStartOnce = true;
                                Station_转盘下料.Instance.IsStartOnce = true;
                                Step = StationStep.机械手放料完成;
                                break;
                            }
                            Step = StationStep.允许机械手离开;
                        }
                        break;

                    case StationStep.允许机械手离开:
                        if(PLCWriteMethod(logBaseInfo, (Common.PLCAdr.DicOutPutBool[PLCOutPutBool.上料离开允许], true, null)))
                        {
                            Step = StationStep.等待机械手离开;
                        }
                        break;

                    case StationStep.等待机械手离开:
                        if(Common.PLCAdr.DicInPutValue[PLCInPut.上料离开] || Common.BP_屏蔽PLC)
                        {
                            Step = StationStep.机械手放料完成;
                        }
                        break;

                    case StationStep.机械手放料完成:
                        ValueQueue.Enqueue(new MeasureProduct() { TestStart = DateTime.Now });
                        Common.ChangeHoleStatus(CurHoleId, !IsGrr反向取料);
                        PLCWriteMethod(logBaseInfo, (Common.PLCAdr.DicOutPutBool[PLCOutPutBool.上料放料允许], false, null),
                                                    (Common.PLCAdr.DicOutPutBool[PLCOutPutBool.上料离开允许], false, null));
                        Step = StationStep.等待交互信号关闭;
                        break;

                    case StationStep.等待交互信号关闭:
                        if((!Common.PLCAdr.DicInPutValue[PLCInPut.上料到位] && !Common.PLCAdr.DicInPutValue[PLCInPut.上料离开]) || Common.BP_屏蔽PLC)
                        {
                            if(IsGrr反向取料)
                            {
                                IsGrr反向取料 = false;
                                if(Common.CurGRR循环次数 > Common.IP_GRR次数)
                                {
                                    Common.CurGRR循环次数 = 1;

                                    Common.OmronPLC.Write(Common.PLCAdr.DicOutPutBool[PLCOutPutBool.下料取料允许2], false);
                                    if(CurHoleId == 8)
                                    {
                                        Common.DicHoleShield[CurHoleId] = true;
                                        Common.DicHoleShield[1] = false;
                                        MachineStatusManager.CurrentMachineStatus = MachineStatus.Pause;
                                        Common.GRR循环模式 = false;
                                        //if (Common.PLCAdr.DicInPutValue[PLCInPut.上料安全信号])
                                        //{
                                            //Common.OmronPLC.Write(Common.PLCAdr.DicOutPutBool[PLCOutPutBool.GRR模式], false);
                                        //}
                                        MessageBox.Show("GGR流程完成");
                                    }
                                    else
                                    {
                                        Common.DicHoleShield[CurHoleId] = true;
                                        Common.DicHoleShield[CurHoleId + 1] = false;
                                    }
                                    Step = StationStep.工位流程完成;
                                    break;
                                }
                                else
                                {
                                    Step = StationStep.允许机械手放料;
                                    break;
                                }
                            }
                            Step = StationStep.工位流程完成;
                        }
                        break;

                    case StationStep.工位流程完成:
                        CurHoleId++;
                        if(CurHoleId > 8)
                        {
                            CurHoleId = 1;
                        }
                        Common.ChangeTurnTableStatus(StationID, true);
                        Step = StationStep.判断是否工作;
                        break;

                    default:
                        break;
                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();
        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            判断是否工作,
            允许机械手放料,
            等待机械手放料,
            打开真空吸,
            允许机械手离开,
            等待机械手离开,
            机械手放料完成,
            等待交互信号关闭,
            工位流程完成
        }
    }

}
